<?php 

require_once("connect.php");
/*
$current_time = time();

$has_location = trim($_POST[id]) != "";

if ($has_location) {

	$assoc_id = trim($_POST[assoc_id]) == "None" ? "NULL" : "'$_POST[assoc_id]'";

	$sql = "INSERT INTO location (id, assoc_id, van, type, address, 
								  num_people, total_wait, start_time, 
								  last_updated)
			VALUES ('$_POST[id]', $assoc_id, '$_POST[van]', '$_POST[type]', 
				    '$_POST[address]', '$_POST[num_people]', 0, 
				    $current_time, $current_time);
	";

	$insert = mysql_query($sql) or die(mysql_error());

	$sql = "UPDATE geocode SET access_frequency = access_frequency + 1 WHERE full_address = '$_POST[address]'";
	$update = mysql_query($sql) or die(mysql_error());
}
*/

$current_time = time();

$has_pickup = trim($_POST[pickup])!='';
$has_dropoff = trim($_POST[dropoff])!='';

$ids = array();

if ($has_pickup) {

	$sql = "INSERT INTO location (assoc_id, van, type, address, 
								  num_people, total_wait, start_time, 
								  last_updated)
			VALUES (NULL, $_POST[van], 'PICKUP', '$_POST[pickup]', 
				    '$_POST[size]', 0, $current_time, $current_time);
	";

	$insert_pickup = mysql_query($sql) or die(mysql_error());
	$pickup_id = mysql_insert_id();
	$ids[] = $pickup_id;
}

if ($has_dropoff) {
	$sql = "INSERT INTO location (assoc_id, van, type, address, 
								  num_people, total_wait, start_time, 
								  last_updated)
			VALUES (NULL, $_POST[van], 'DROPOFF', '$_POST[dropoff]', 
				    '-$_POST[size]', 0, $current_time, $current_time);
	";

	$insert_dropoff = mysql_query($sql) or die(mysql_error());
	$dropoff_id = mysql_insert_id();
	$ids[] = $dropoff_id;
}

if ($has_dropoff && $has_pickup) {
	$update_pickup = "UPDATE location SET assoc_id = '$dropoff_id' WHERE id = '$pickup_id'";
	$update_dropoff = "UPDATE location SET assoc_id = '$pickup_id' WHERE id = '$dropoff_id'";

	mysql_query($update_pickup) or die(mysql_error());
	mysql_query($update_dropoff) or die(mysql_error());
}

echo json_encode($ids);

touch('../driverside/last_updated');

?>